/*
 * scheduleAlgorithm.c
 *
 *  Created on: 13.01.2013
 *      Author: Michael
 */

/*
 * ======== Project related includes ========
 */
#include "os/scheduler/scheduleAlgorithm.h"
#include "os/sysprops.h"

uint16_t scheduleAlgo_roundRobin(Thread_t* allThreads, uint16_t currThread){
	uint16_t nextThread = currThread + 1;
	if (nextThread >= SCHEDULER_MAX_THREADS) {
		nextThread = 0;
	}

	if (allThreads[nextThread].state != THREAD_INVALID) {
		return nextThread;
	} else {
		// find next thread to run
		uint16_t i;
		for (i = nextThread; i < SCHEDULER_MAX_THREADS; ++i) {
			if (allThreads[i].state == THREAD_READY) {
				return i;
			}
		}
	}

	return 0;
}
